#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

class TopicSubscribe01 : public rclcpp::Node{
public:
    //构造函数，有一个参数为节点名称
    TopicSubscribe01(std::string name) : Node(name){
        RCLCPP_INFO(this->get_logger(),"大家好啊，我是%s",name.c_str());
        command_subscribe_ = this ->create_subscription<std_msgs::msg::String>("command",10,std::bind(&TopicSubscribe01::command_callback,this,std::placeholders::_1));
    }
private:
    //声明一个订阅者
    rclcpp::Subscription<std_msgs::msg::String>::SharedPtr command_subscribe_;
    //收到话题数据的回调参数
    void command_callback(const std_msgs::msg::String::SharedPtr msg){
        double speed = 0.0f;
        if(msg->data == "forward"){
            speed = 0.2f;
        }
        RCLCPP_INFO(this->get_logger(),"收到[%s]指令,发送速度%f",msg->data.c_str(),speed);
    }
};

int main(int argc,char **argv){
    rclcpp::init(argc,argv);
    //创建对应节点的共享指针对象
    auto node = std::make_shared<TopicSubscribe01>("topic_subscribe_01");
    //运行节点并检测退出信号
    rclcpp::spin(node);
    rclcpp::shutdown();
    return 0;
}